﻿//------------------------------------------------------------------------------
// ╭─────────────────────────────╮
// │ ╭─╮     ╭─╮              TM │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// │ │ │     │ │                 │                                           ☺  
// │ │ ╰───╮ │ │ ╭─────╮ ╭─────╮ │     Name:Wedn.Net 管理页面基控制器        ♮ ♪ ♩
// │ │ ╭─╮ │ │ │ │ ╭─╮ │ │ ╭─╮ │ │                                    ♫ ♭      
// │ │ ╰─╯ │ │ │ │ ╰─╯ │ │ ╰─╯ │ │     Author:iceStone               ♬ ♪       
// │ └─────╯ └─╯ ╰─────╯ ╰───╮ │ │     Chinese:汪磊                              
// │                     ┌───╯ │ │                                              
// │                     ╰─────╯ │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// ╰─────────────────────────────╯                                              
//------------------------------------------------------------------------------
using System;
using System.Web.Mvc;
using Wedn.Net.Model;
using Wedn.Net.Utility;

namespace Wedn.Net.UI.Controllers
{
    /// <summary>
    /// 管理页面基控制器
    /// </summary>
    public class AdminBaseController : BaseController
    {
        private User _currentUser;
        /// <summary>
        /// 当前用户对象实体
        /// </summary>
        protected User CurrentUser
        {
            get { return _currentUser ?? (_currentUser = SessionHelper.GetSession<User>("CurrentUser")); }
        }
        ///// <summary>
        ///// 视图根路径
        ///// </summary>
        //protected string ViewRootPath { get { return @"~\Views\Admin\"; } }
        /// <summary>
        /// Action执行事件
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            if (!BLLFactory.BLLFactory.Instance.UserManager.CheckLogin())
            {
                filterContext.HttpContext.Response.Redirect("/Login?redirect=" + StringHelper.UrlEncode(filterContext.HttpContext.Request.Url.ToString()));
                return;
            }
            CookieHelper.SetCookie("SessionId", CookieHelper.GetCookie("SessionId"), DateTime.Now.AddMinutes(30));

            ViewBag.CurrentUser = CurrentUser;

            #region old 2.0
            ////获取当前请求的FilePath
            //string filePath = filterContext.HttpContext.Request.FilePath.ToLower();
            //if (filePath != "/admin/login")
            //{
            //    if (!BLLFactory.BLLFactory.Instance.UserManager.CheckLogin())
            //    {
            //        filterContext.HttpContext.Response.Redirect("/Admin/Login?redirect=" + filterContext.HttpContext.Request.Url);
            //        return;
            //    }
            //    else
            //        CookieHelper.SetCookie("SessionId", CookieHelper.GetCookie("SessionId"), DateTime.Now.AddMinutes(30));
            //}
            //else
            //{
            //    if (BLLFactory.BLLFactory.Instance.UserManager.CheckLogin())
            //    {
            //        var url = RequestHelper.QueryString("redirect");
            //        url = string.IsNullOrEmpty(url) ? "/" : url;
            //        //用户已登陆 //校验Cookie登陆；
            //        filterContext.HttpContext.Response.Redirect(url);
            //        return;
            //    }
            //}
            //ViewBag.CurrentUser = CurrentUser; 
            #endregion

            #region old
            ////RawUrl如果是登录界面的话  放行；
            //if (filePath != "/admin/login")
            //{
            //    //不是则进行权限校验
            //    //CurrentUser = Session["CurrentUser"] as User;
            //    if (CurrentUser == null)
            //    {
            //        //校验Cookie登陆；
            //        if (!BLLFactory.BLLFactory.GetBLLFactory().UserManager.CookieLogin())
            //        {
            //            filterContext.HttpContext.Response.Redirect("~/Admin/Login?redirect=" + filterContext.HttpContext.Request.Url);
            //            return;
            //        }
            //    }
            //    else
            //    {
            //        var sessionId = CookieHelper.GetCookie("SessionId");
            //        CookieHelper.SetCookie("SessionId", sessionId, DateTime.Now.AddMinutes(30));
            //    }
            //}
            //else
            //{
            //    var url = RequestHelper.QueryString("r");
            //    url = string.IsNullOrEmpty(url) ? "/Admin/" : url;

            //    if (CurrentUser != null)
            //    {
            //        //用户已登陆
            //        filterContext.HttpContext.Response.Redirect(url);
            //        return;
            //    }
            //    //校验Cookie登陆；
            //    if (BLLFactory.BLLFactory.GetBLLFactory().UserManager.CookieLogin())
            //    {
            //        filterContext.HttpContext.Response.Redirect(url);
            //        return;
            //    }
            //}
            //ViewBag.CurrentUser = CurrentUser; 
            #endregion

            base.OnActionExecuting(filterContext);
        }
    }
}
